<?php
/**
 * Created by PhpStorm.
 * User: songdewei
 * Date: 2017/10/10
 * Time: 下午3:43
 */

namespace App\WeChat\WxApi;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

class WxMaterialApi extends WxApi
{
    /**
     * @param $type
     * @param $data
     * @return mixed
     * @throws \Exception
     */
    public static function add($type, $data){
        $access_token = self::getAccessToken();
        $uri = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_token&type=$type";
        $request = new Request('POST', $uri, [], $data);
        $client  = new Client();
        $response = $client->send($request);

        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }

        return false;
    }

    /**
     * @param $media_id
     * @return mixed
     * @throws \Exception
     */
    public static function delete($media_id){
        $access_data  = json_encode(['media_id'=>$media_id]);
        $access_token = self::getAccessToken();
        $uri = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=$access_token";
        $request = new Request('POST', $uri, [], $access_data);
        $client  = new Client();
        $response = $client->send($request);
        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }
        return false;
    }

    /**
     * @param $media_id
     * @return mixed
     * @throws \Exception
     */
    public static function get($media_id){
        $access_data = json_encode(['media_id'=>$media_id]);
        $uri = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=".self::getAccessToken();
        $request = new Request('POST', $uri, [], $access_data);

        $client = new Client();
        $response = $client->send($request);
        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }
        return false;
    }

    /**
     * @return mixed
     * @throws \Exception
     */
    public static function getCount(){
        $client = new Client();
        $response = $client->get("https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=".self::getAccessToken());
        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }
        return false;
    }

    /**
     * @param string $type
     * @param int $offset
     * @param int $count
     * @return mixed
     * @throws \Exception
     */
    public static function batchget($type='image', $offset=0, $count=20){
        $access_data = json_encode([
            'type'=>$type,
            'offset'=>$offset,
            'count'=>$count
        ]);
        $uri = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=".self::getAccessToken();
        $request = new Request('POST', $uri, [], $access_data);
        $client  = new Client();
        $response = $client->send($request);
        if ($response->getStatusCode() == 200) {
            return $response->getBody();
        }
        return false;
    }
}
